package com.netease.nie.facade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.base.utils.CustomDateFormatUtils;
import com.netease.nie.base.utils.PageUtils;
import com.netease.nie.common.CommonBusinessTools;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.constants.CustomGoodsConstants;
import com.netease.nie.constants.DisplayTypeConstants;
import com.netease.nie.constants.GoodsTypeStants;
import com.netease.nie.constants.RobGoodsPrizeTipsConstants;
import com.netease.nie.dto.AddressDto;
import com.netease.nie.dto.CustomGoodsPrizeDtoAddressInfoDto;
import com.netease.nie.dto.CustomGoodsPrizeLogisticsInfoDto;
import com.netease.nie.dto.CustomGoodsPrizeOrdersTraceInfoDto;
import com.netease.nie.dto.DrawLotteryGoodsPrizeBaseInfoDto;
import com.netease.nie.dto.ExchangeCodeDto;
import com.netease.nie.dto.LogisticsDto;
import com.netease.nie.dto.MyDrawLotteryGoodsPrizeDto;
import com.netease.nie.dto.MyLuckyPrizeDto;
import com.netease.nie.dto.MyRobGoodsPrizeDto;
import com.netease.nie.dto.MyRobGoodsPrizeTipsDto;
import com.netease.nie.dto.OrdersAddressDto;
import com.netease.nie.dto.OrdersTraceDto;
import com.netease.nie.dto.RobGoodsInfoDto;
import com.netease.nie.dto.RobGoodsInfoPeriodDto;
import com.netease.nie.dto.RobGoodsPeriodUserPersonTimeDto;
import com.netease.nie.dto.RobGoodsPrizeBaseInfoDto;
import com.netease.nie.model.DrawLotteryNormalGoodsInfo;
import com.netease.nie.model.ExchangeCode;
import com.netease.nie.model.Orders;
import com.netease.nie.service.AddressService;
import com.netease.nie.service.DrawLotteryNormalGoodsInfoService;
import com.netease.nie.service.ExchangeCodeService;
import com.netease.nie.service.LogisticsService;
import com.netease.nie.service.OrderService;
import com.netease.nie.service.OrdersAddressService;
import com.netease.nie.service.OrdersTraceService;
import com.netease.nie.service.RobGoodsInfoPeriodService;
import com.netease.nie.service.RobGoodsInfoService;
import com.netease.nie.service.RobGoodsPeriodPersonTimeService;

@Service
public class MyLuckyPrizeFacadeImpl extends CommonBusinessTools implements MyLuckyPrizeFacade {

	private static final int PAGE_SIZE = 20;
	
	private static final String ROB_GOODS_PRIZE_BASE_INFO_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_PRIZE_BASE_INFO_PREFIX;
	
	private static final String USER_ROB_PRIZE_PREFIX = CachePrefixConstants.UserPrefix.USER_ROB_PRIZE_PREFIX;
	
	@Autowired
	private RobGoodsInfoService robGoodsInfoService;
	
	@Autowired
	private RobGoodsInfoPeriodService robGoodsInfoPeriodService;
	
	@Autowired
	private RobGoodsPeriodPersonTimeService robGoodsPeriodPersonTimeService;
	
	@Autowired
	private DrawLotteryNormalGoodsInfoService drawLotteryNormalGoodsInfoService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrdersAddressService OrdersAddressService;
	
	@Autowired
	private OrdersTraceService ordersTraceService;
	
	@Autowired
	private ExchangeCodeService exchangeCodeService;
	
	@Autowired
	private LogisticsService logisticsService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private Redis redis;
	
	public MyRobGoodsPrizeDto queryRobGoodsPrizeDtoByCondition(long userId, long orderId, int robGoodsId, int displayPeriod) {
		MyRobGoodsPrizeDto myRobGoodsPrizeDto = new MyRobGoodsPrizeDto();
		
		//商品信息
		RobGoodsPrizeBaseInfoDto robGoodsPrizeBaseInfoDto = null;
		String robGoodsPrizeBaseInfoKey = ROB_GOODS_PRIZE_BASE_INFO_PREFIX + robGoodsId;
		boolean robGoodsPrizeBaseInfoKeyExists = redis.exists(robGoodsPrizeBaseInfoKey);
		if (!robGoodsPrizeBaseInfoKeyExists) {
			RobGoodsInfoDto robGoodsInfoDto = robGoodsInfoService.queryRobGoodsInfoDtoByRobGoodsId(robGoodsId);
			RobGoodsInfoPeriodDto robGoodsInfoPeriodDto = robGoodsInfoPeriodService.queryRobGoodsInfoPeriodDtoByDisplayPeriod(displayPeriod);
			if (robGoodsInfoDto != null) {
				robGoodsPrizeBaseInfoDto = new RobGoodsPrizeBaseInfoDto();
				robGoodsPrizeBaseInfoDto.setDisplayPeriod(displayPeriod);
				robGoodsPrizeBaseInfoDto.setRobGoodsId(robGoodsId);
				robGoodsPrizeBaseInfoDto.setRobGoodsTitle(robGoodsInfoDto.getTitle());
				robGoodsPrizeBaseInfoDto.setThumbnailWapUrl(robGoodsInfoDto.getThumbnailWapUrl());
				robGoodsPrizeBaseInfoDto.setThumbnailWebUrl(robGoodsInfoDto.getThumbnailWebUrl());
				robGoodsPrizeBaseInfoDto.setGoodsType(robGoodsInfoDto.getGoodsType());
				robGoodsPrizeBaseInfoDto.setGoodsId(robGoodsInfoDto.getGoodsId());
			}
			if (robGoodsInfoPeriodDto != null) 
				robGoodsPrizeBaseInfoDto.setRobUserPt(robGoodsInfoPeriodDto.getRobUserPt());
			redis.set(robGoodsPrizeBaseInfoKey, robGoodsPrizeBaseInfoDto);
		} else
			robGoodsPrizeBaseInfoDto = redis.get(robGoodsPrizeBaseInfoKey, RobGoodsPrizeBaseInfoDto.class);
		
		if (robGoodsPrizeBaseInfoDto != null)
			replaceRobGoodsPrizeBaseInfoDto(robGoodsPrizeBaseInfoDto, DisplayTypeConstants.WAP.getCode());
		myRobGoodsPrizeDto.setRobGoodsPrizeBaseInfoDto(robGoodsPrizeBaseInfoDto);
		
		if (robGoodsPrizeBaseInfoDto != null) {
			int goodsType = robGoodsPrizeBaseInfoDto.getGoodsType();
			//兑换码信息
			if (goodsType == GoodsTypeStants.REDEEM_CODE.getCode()) {
				Orders orders = orderService.getOrderByOrderIdAndUserId(orderId, userId);
				Integer exchangeCodeId = orders.getExchangeCodeId();
				ExchangeCodeDto exchangeCodeDto = new ExchangeCodeDto();
				if (exchangeCodeId != null) {
					ExchangeCode exchageCode = exchangeCodeService.queryExchangeCodeById(orders.getExchangeCodeId());
					CopyUtils.copy(exchangeCodeDto, exchageCode);
				}
				myRobGoodsPrizeDto.setExchangeCodeDto(exchangeCodeDto);
				return myRobGoodsPrizeDto;
			} else if (goodsType == GoodsTypeStants.ENTITY.getCode()) {
				OrdersAddressDto ordersAddressDto = OrdersAddressService.queryOrdersAddressByOrdersId(orderId);
				CustomGoodsPrizeDtoAddressInfoDto customGoodsPrizeDtoAddressInfoDto = new CustomGoodsPrizeDtoAddressInfoDto();
				if (ordersAddressDto == null) {
					AddressDto addressDto = addressService.queryDefaultAddressByUserId(userId);
					if (addressDto != null)
						CopyUtils.copy(customGoodsPrizeDtoAddressInfoDto, addressDto);
					
					customGoodsPrizeDtoAddressInfoDto.setOrderId(orderId);
					myRobGoodsPrizeDto.setCustomGoodsPrizeDtoAddressInfoDto(customGoodsPrizeDtoAddressInfoDto);
					return myRobGoodsPrizeDto;
				}
				
				CopyUtils.copy(customGoodsPrizeDtoAddressInfoDto, ordersAddressDto);
				myRobGoodsPrizeDto.setCustomGoodsPrizeDtoAddressInfoDto(customGoodsPrizeDtoAddressInfoDto);
				List<OrdersTraceDto> ordersTraceDtoList = ordersTraceService.queryOrdersTraceListByOrdersId(orderId);
				List<CustomGoodsPrizeOrdersTraceInfoDto> customGoodsPrizeOrdersTraceInfoDtoList = generateCustomGoodsPrizeOrdersTraceInfoDtoList(ordersTraceDtoList);
				if (customGoodsPrizeOrdersTraceInfoDtoList != null && !customGoodsPrizeOrdersTraceInfoDtoList.isEmpty())
					myRobGoodsPrizeDto.setCustomGoodsPrizeOrdersTraceInfoDtos(customGoodsPrizeOrdersTraceInfoDtoList);
				LogisticsDto logisticsDto = logisticsService.queryLogisticsByOrdersId(orderId);
				if (logisticsDto != null) {
					CustomGoodsPrizeLogisticsInfoDto customGoodsPrizeLogisticsInfoDto = new CustomGoodsPrizeLogisticsInfoDto();
					CopyUtils.copy(customGoodsPrizeLogisticsInfoDto, logisticsDto);
					myRobGoodsPrizeDto.setCustomGoodsPrizeLogisticsInfoDto(customGoodsPrizeLogisticsInfoDto);
				}
					
			}
		}
		return myRobGoodsPrizeDto;
	}

	public List<MyLuckyPrizeDto> queryMyLuckyPrizeDtosByCondition(long userId, int page, int type) {
		int pageIndex = PageUtils.getPageStartIndex(page, PAGE_SIZE);
		List<MyLuckyPrizeDto> myLuckyPrizeDtoList = new ArrayList<>();
		Orders orders = null;
		List<Orders> orderList = null;
		switch (type) {
		case 1: {
			orderList = orderService.queryOrdersByUserIdAndCustomGoodsCode(userId, CustomGoodsConstants.ROB, pageIndex, PAGE_SIZE);
			if (orderList == null || orderList.isEmpty())
				return Collections.emptyList();
			
			List<Integer> robGoodsIdList = new ArrayList<>();
			List<Integer> robGoodsPeriodIdList = new ArrayList<>();
			List<Long> orderIdList = new ArrayList<>();
			List<Integer> exchangeCodeIdList = new ArrayList<>();
			int orderListSize = orderList.size();
			for (int i = 0; i < orderListSize; i++) {
				orders = orderList.get(i);
				Integer exchangeCodeId = orders.getExchangeCodeId();
				if (exchangeCodeId != null)
					exchangeCodeIdList.add(exchangeCodeId);
				int customGoodsId = orders.getCustomGoodsId();
				if (!robGoodsIdList.contains(customGoodsId))
					robGoodsIdList.add(orders.getCustomGoodsId());
				robGoodsPeriodIdList.add(orders.getPeriodId());
				orderIdList.add(orders.getId());
			}
			
			Map<Integer, ExchangeCode> exchangeCodeMap = exchangeCodeService.queryExchangeCodeByIds(exchangeCodeIdList);
			Map<Integer, RobGoodsInfoDto> robGoodsInfoDtoMap = robGoodsInfoService.queryRobGoodsInfoDtoMapByRobGoodsIds(robGoodsIdList);
			List<RobGoodsPeriodUserPersonTimeDto> robGoodsPeriodUserPersonTimeDtoList = robGoodsPeriodPersonTimeService.queryRobGoodsPeriodUserPersonTimeDtosByUserIdAndRobGoodsPeriodIds(userId, robGoodsPeriodIdList);
			Map<Integer, RobGoodsPeriodUserPersonTimeDto> robUserPtMap = generateRobUserDisplayPeriodPtMap(robGoodsPeriodUserPersonTimeDtoList);
			List<OrdersTraceDto> ordersTraceDtoList = ordersTraceService.queryCurrentStatusOrdersTraceDtosByOrderIds(orderIdList);
			Map<Long, Integer> ordersTraceMap = distillOrdersTraceMap(ordersTraceDtoList);
			generateMyLuckyRobPrizeDtoList(myLuckyPrizeDtoList, orderList, robGoodsInfoDtoMap, exchangeCodeMap, robUserPtMap, ordersTraceMap);
		}
		break;

		case 2 : {
			orderList = orderService.queryOrdersByUserIdAndCustomGoodsCode(userId, CustomGoodsConstants.DRAW_LOTTERY, pageIndex, PAGE_SIZE);
			if (orderList == null || orderList.isEmpty())
				return Collections.emptyList();
			
			List<Integer> exchangeCodeIdList = new ArrayList<>();
			List<Integer> drawLotteryNormalGoodsIdList = new ArrayList<>();
			List<Long> orderIdList = new ArrayList<>();
			int orderListSize = orderList.size();
			for (int i = 0; i < orderListSize; i++) {
				orders = orderList.get(i);
				Integer exchangeCodeId = orders.getExchangeCodeId();
				int customGoodsId = orders.getCustomGoodsId();
				if (exchangeCodeId != null)
					exchangeCodeIdList.add(exchangeCodeId);
				if (!drawLotteryNormalGoodsIdList.contains(customGoodsId))
					drawLotteryNormalGoodsIdList.add(orders.getCustomGoodsId());
				orderIdList.add(orders.getId());
			}
			Map<Integer, ExchangeCode> exchangeCodeMap = exchangeCodeService.queryExchangeCodeByIds(exchangeCodeIdList);
			Map<Integer, DrawLotteryNormalGoodsInfo> drawLotteryNormalGoodsInfoMap = drawLotteryNormalGoodsInfoService.queryDrawLotteryNormalGoodsInfoMapByNormalGoodsIds(drawLotteryNormalGoodsIdList);
			List<OrdersTraceDto> ordersTraceDtoList = ordersTraceService.queryCurrentStatusOrdersTraceDtosByOrderIds(orderIdList);
			Map<Long, Integer> ordersTraceMap = distillOrdersTraceMap(ordersTraceDtoList);
			generateMyLuckyDrawLotteryPrizeDtoList(myLuckyPrizeDtoList, orderList, drawLotteryNormalGoodsInfoMap, exchangeCodeMap, ordersTraceMap);
		}
		break;
		
		default: 
			break;
		}

		replaceMyLuckyPrizeDtoThumbnailUrl(myLuckyPrizeDtoList, DisplayTypeConstants.WAP.getCode());
		return myLuckyPrizeDtoList;
	}
	
	private Map<Long, Integer> distillOrdersTraceMap(List<OrdersTraceDto> orderTraceDtoList) {
		if (orderTraceDtoList != null && !orderTraceDtoList.isEmpty()) {
			Map<Long, Integer> ordersTraceMap = new HashMap<Long, Integer>();
			int orderTraceDtoListSize = orderTraceDtoList.size();
			OrdersTraceDto ordersTraceDto = null;
			for (int i = 0; i < orderTraceDtoListSize; i++) {
				ordersTraceDto = orderTraceDtoList.get(i);
				long orderId = ordersTraceDto.getOrderId();
				if (ordersTraceMap.containsKey(orderId)) {
					int status = ordersTraceMap.get(orderId);
					if (status < ordersTraceDto.getStatus())
						ordersTraceMap.put(ordersTraceDto.getOrderId(), ordersTraceDto.getStatus());
				} else
					ordersTraceMap.put(ordersTraceDto.getOrderId(), ordersTraceDto.getStatus());
			}
			return ordersTraceMap;
		}
		return Collections.emptyMap();
	}
	
	private void generateMyLuckyRobPrizeDtoList(List<MyLuckyPrizeDto> myLuckyPrizeDtoList, 
			List<Orders> orderList, 
			Map<Integer, RobGoodsInfoDto> robGoodsInfoDtoMap, 
			Map<Integer, ExchangeCode> exchangeCodeMap, 
			Map<Integer, RobGoodsPeriodUserPersonTimeDto> robUserPtMap, 
			Map<Long, Integer> ordersTraceMap) {
		Orders orders = null;
		ExchangeCode exchangeCode = null;
		RobGoodsInfoDto robGoodsInfoDto = null;
		RobGoodsPeriodUserPersonTimeDto robGoodsPeriodUserPersonTimeDto = null;
		MyLuckyPrizeDto myLuckyPrizeDto = null;
		int orderListSize = orderList.size();
		for (int i = 0; i < orderListSize; i++) {
			orders = orderList.get(i);
			myLuckyPrizeDto = new MyLuckyPrizeDto();
			int robGoodsId = orders.getCustomGoodsId();
			int robGoodsPeriodId = orders.getPeriodId();
			Integer exchangeCodeId = orders.getExchangeCodeId();
			long orderId = orders.getId();
			myLuckyPrizeDto.setOrderId(orderId);
			myLuckyPrizeDto.setRobGoodsId(robGoodsId);
			if (robGoodsInfoDtoMap.containsKey(robGoodsId)) {
				robGoodsInfoDto = robGoodsInfoDtoMap.get(robGoodsId);
				myLuckyPrizeDto.setRobGoodsTitle(robGoodsInfoDto.getTitle());
				myLuckyPrizeDto.setThumbnailWapUrl(robGoodsInfoDto.getThumbnailWapUrl());
				myLuckyPrizeDto.setThumbnailWebUrl(robGoodsInfoDto.getThumbnailWebUrl());
			}
			if (robUserPtMap.containsKey(robGoodsPeriodId)) {
				robGoodsPeriodUserPersonTimeDto = robUserPtMap.get(robGoodsPeriodId);
				myLuckyPrizeDto.setDisplayPeriod(robGoodsPeriodUserPersonTimeDto.getDisplayPeriod());
				myLuckyPrizeDto.setRobUserPt(robGoodsPeriodUserPersonTimeDto.getRobUserPt());
			}
			
			if (orders.getGoodsType() == GoodsTypeStants.REDEEM_CODE.getCode())
				myLuckyPrizeDto.setStatus(-1);
			else if (orders.getGoodsType() == GoodsTypeStants.ENTITY.getCode()) {
				if (ordersTraceMap.containsKey(orderId)) {
					int status = ordersTraceMap.get(orderId);
					myLuckyPrizeDto.setStatus(status);
				} else
					myLuckyPrizeDto.setStatus(0);
			}
				
			if (exchangeCodeMap.containsKey(exchangeCodeId)) {
				exchangeCode = exchangeCodeMap.get(exchangeCodeId);
				myLuckyPrizeDto.setExchangeCode(exchangeCode.getExchangeCode());
			}
			myLuckyPrizeDtoList.add(myLuckyPrizeDto);
		}
	}
	
	private void generateMyLuckyDrawLotteryPrizeDtoList(List<MyLuckyPrizeDto> myLuckyPrizeDtoList, 
			List<Orders> orderList, 
			Map<Integer, DrawLotteryNormalGoodsInfo> drawLotteryNormalGoodsInfoMap, 
			Map<Integer, ExchangeCode> exchangeCodeMap, 
			Map<Long, Integer> ordersTraceMap) {
		Orders orders = null;
		ExchangeCode exchangeCode = null;
		DrawLotteryNormalGoodsInfo drawLotteryNormalGoodsInfo = null;
		MyLuckyPrizeDto myLuckyPrizeDto = null;
		int orderListSize = orderList.size();
		for (int i = 0; i < orderListSize; i++) {
			orders = orderList.get(i);
			myLuckyPrizeDto = new MyLuckyPrizeDto();
			int drawLotteryNormalGoodsId = orders.getCustomGoodsId();
			Integer exchangeCodeId = orders.getExchangeCodeId();
			long orderId = orders.getId();
			myLuckyPrizeDto.setOrderId(orderId);
			myLuckyPrizeDto.setDrawLotteryGoodsId(drawLotteryNormalGoodsId);
			if (drawLotteryNormalGoodsInfoMap.containsKey(drawLotteryNormalGoodsId)) {
				drawLotteryNormalGoodsInfo = drawLotteryNormalGoodsInfoMap.get(drawLotteryNormalGoodsId);
				myLuckyPrizeDto.setDrawLotteryGoodsTitle(drawLotteryNormalGoodsInfo.getTitle());
				myLuckyPrizeDto.setThumbnailWapUrl(drawLotteryNormalGoodsInfo.getThumbnailWapUrl());
				myLuckyPrizeDto.setThumbnailWebUrl(drawLotteryNormalGoodsInfo.getThumbnailWebUrl());
			}
			if (exchangeCodeMap.containsKey(exchangeCodeId)) {
				exchangeCode = exchangeCodeMap.get(exchangeCodeId);
				myLuckyPrizeDto.setExchangeCode(exchangeCode.getExchangeCode());
			}
			if (ordersTraceMap.containsKey(orderId)) {
				int status = ordersTraceMap.get(orderId);
				myLuckyPrizeDto.setStatus(status);
			} else 
				myLuckyPrizeDto.setStatus(-1);
			myLuckyPrizeDtoList.add(myLuckyPrizeDto);
		}
	}

	private List<CustomGoodsPrizeOrdersTraceInfoDto> generateCustomGoodsPrizeOrdersTraceInfoDtoList(List<OrdersTraceDto> ordersTraceDtoList) {
		if (ordersTraceDtoList != null && !ordersTraceDtoList.isEmpty()) {
			List<CustomGoodsPrizeOrdersTraceInfoDto> customGoodsPrizeOrdersTraceInfoDtoList = new ArrayList<>();
			int ordersTraceDtoListSize = ordersTraceDtoList.size();
			OrdersTraceDto ordersTraceDto = null;
			CustomGoodsPrizeOrdersTraceInfoDto customGoodsPrizeOrdersTraceInfoDto = null;
			for (int i = 0; i < ordersTraceDtoListSize; i++) {
				ordersTraceDto = ordersTraceDtoList.get(i);
				customGoodsPrizeOrdersTraceInfoDto = new CustomGoodsPrizeOrdersTraceInfoDto();
				CopyUtils.copy(customGoodsPrizeOrdersTraceInfoDto, ordersTraceDto);
				customGoodsPrizeOrdersTraceInfoDto.setCreateTimeStr(CustomDateFormatUtils.getyyyyMMddHHmmssStr(ordersTraceDto.getCreateTime()));
				customGoodsPrizeOrdersTraceInfoDtoList.add(customGoodsPrizeOrdersTraceInfoDto);
			}
			return customGoodsPrizeOrdersTraceInfoDtoList;
		}
		return Collections.emptyList();
	}

	public MyDrawLotteryGoodsPrizeDto queryMyDrawLotteryGoodsPrizeDtoByCondition(
			long userId, long orderId, int drawLotteryNormalGoodsId) {
		MyDrawLotteryGoodsPrizeDto myDrawLotteryGoodsPrizeDto = new MyDrawLotteryGoodsPrizeDto();
		
		//商品信息
		DrawLotteryGoodsPrizeBaseInfoDto drawLotteryGoodsPrizeBaseInfoDto = null;
		DrawLotteryNormalGoodsInfo drawLotteryNormalGoodsInfo = drawLotteryNormalGoodsInfoService.queryDrawLotteryNormalGoodsInfoById(drawLotteryNormalGoodsId);
		if (drawLotteryNormalGoodsInfo != null) {
			drawLotteryGoodsPrizeBaseInfoDto = new DrawLotteryGoodsPrizeBaseInfoDto();
			drawLotteryGoodsPrizeBaseInfoDto.setDrawLotteryGoodsId(drawLotteryNormalGoodsId);
			drawLotteryGoodsPrizeBaseInfoDto.setDrawLotteryGoodsTitle(drawLotteryNormalGoodsInfo.getTitle());
			CopyUtils.copy(drawLotteryGoodsPrizeBaseInfoDto, drawLotteryNormalGoodsInfo);
			if (drawLotteryGoodsPrizeBaseInfoDto != null)
				replaceDrawLotteryGoodsPrizeBaseInfoDto(drawLotteryGoodsPrizeBaseInfoDto, DisplayTypeConstants.WAP.getCode());
			myDrawLotteryGoodsPrizeDto.setDrawLotteryGoodsPrizeBaseInfoDto(drawLotteryGoodsPrizeBaseInfoDto);
		}
		
		if (drawLotteryGoodsPrizeBaseInfoDto != null) {
			int goodsType = drawLotteryGoodsPrizeBaseInfoDto.getGoodsType();
			//兑换码信息
			if (goodsType == GoodsTypeStants.REDEEM_CODE.getCode()) {
				Orders orders = orderService.getOrderByOrderIdAndUserId(orderId, userId);
				Integer exchangeCodeId = orders.getExchangeCodeId();
				ExchangeCodeDto exchangeCodeDto = new ExchangeCodeDto();
				if (exchangeCodeId != null) {
					ExchangeCode exchageCode = exchangeCodeService.queryExchangeCodeById(orders.getExchangeCodeId());
					CopyUtils.copy(exchangeCodeDto, exchageCode);
				}
				myDrawLotteryGoodsPrizeDto.setExchangeCodeDto(exchangeCodeDto);
				return myDrawLotteryGoodsPrizeDto;
			} else if (goodsType == GoodsTypeStants.ENTITY.getCode()) {
				OrdersAddressDto ordersAddressDto = OrdersAddressService.queryOrdersAddressByOrdersId(orderId);
				CustomGoodsPrizeDtoAddressInfoDto customGoodsPrizeDtoAddressInfoDto = new CustomGoodsPrizeDtoAddressInfoDto();
				if (ordersAddressDto == null) {
					AddressDto addressDto = addressService.queryDefaultAddressByUserId(userId);
					if (addressDto != null)
						CopyUtils.copy(customGoodsPrizeDtoAddressInfoDto, addressDto);
					
					customGoodsPrizeDtoAddressInfoDto.setOrderId(orderId);
					myDrawLotteryGoodsPrizeDto.setCustomGoodsPrizeDtoAddressInfoDto(customGoodsPrizeDtoAddressInfoDto);
					return myDrawLotteryGoodsPrizeDto;
				}
				
				CopyUtils.copy(customGoodsPrizeDtoAddressInfoDto, ordersAddressDto);
				myDrawLotteryGoodsPrizeDto.setCustomGoodsPrizeDtoAddressInfoDto(customGoodsPrizeDtoAddressInfoDto);
				List<OrdersTraceDto> ordersTraceDtoList = ordersTraceService.queryOrdersTraceListByOrdersId(orderId);
				List<CustomGoodsPrizeOrdersTraceInfoDto> customGoodsPrizeOrdersTraceInfoDtoList = generateCustomGoodsPrizeOrdersTraceInfoDtoList(ordersTraceDtoList);
				if (customGoodsPrizeOrdersTraceInfoDtoList != null && !customGoodsPrizeOrdersTraceInfoDtoList.isEmpty())
					myDrawLotteryGoodsPrizeDto.setCustomGoodsPrizeOrdersTraceInfoDtos(customGoodsPrizeOrdersTraceInfoDtoList);
				LogisticsDto logisticsDto = logisticsService.queryLogisticsByOrdersId(orderId);
				if (logisticsDto != null) {
					CustomGoodsPrizeLogisticsInfoDto customGoodsPrizeLogisticsInfoDto = new CustomGoodsPrizeLogisticsInfoDto();
					CopyUtils.copy(customGoodsPrizeLogisticsInfoDto, logisticsDto);
					myDrawLotteryGoodsPrizeDto.setCustomGoodsPrizeLogisticsInfoDto(customGoodsPrizeLogisticsInfoDto);
				}
					
			}
		}
		return myDrawLotteryGoodsPrizeDto;
	}

	public MyRobGoodsPrizeTipsDto queryMyRobGoodsPrizeTipsDtoByUserId(
			long userId) {
		String key = USER_ROB_PRIZE_PREFIX + userId;
		
		MyRobGoodsPrizeTipsDto myRobGoodsPrizeTipsDto = redis.hgetAll(key, MyRobGoodsPrizeTipsDto.class);
		if (myRobGoodsPrizeTipsDto != null) {
			if (myRobGoodsPrizeTipsDto.getStatus() == RobGoodsPrizeTipsConstants.ONE_ROB_GOODS_PRIZE_TIPS.getCode()) {
				int robGoodsId = myRobGoodsPrizeTipsDto.getRobGoodsId();
				RobGoodsInfoDto robGoodsInfoDto = robGoodsInfoService.queryRobGoodsInfoDtoByRobGoodsId(robGoodsId);
				myRobGoodsPrizeTipsDto.setThumbnailWapUrl(robGoodsInfoDto.getThumbnailWapUrl());
				myRobGoodsPrizeTipsDto.setThumbnailWebUrl(robGoodsInfoDto.getThumbnailWebUrl());
				replaceMyRobGoodsPrizeTipsDtoThumbnailUrl(myRobGoodsPrizeTipsDto, DisplayTypeConstants.WAP.getCode());
				redis.hset(key, "status", "0");
			}
		} else {
			myRobGoodsPrizeTipsDto = new MyRobGoodsPrizeTipsDto();
			myRobGoodsPrizeTipsDto.setStatus(RobGoodsPrizeTipsConstants.NO_ROB_GOODS_PRIZE_TIPS.getCode());
		}
			
		return myRobGoodsPrizeTipsDto;
	}
	
}
